import {
  create,
  findMany,
  updateOne,
  deleteOne,
  review,
  findOne,
  exportExcel,

  findStatistic,
  useMaterial,
  findMaterialRecordMany,
  findMaterialRecordByOrderCodeMany, findProcureMaterialLot,
  updateMaterial,
  deleteMaterial, findReturnOrderMany, createReturnGoodsOrder,deleteReturnGoodsOrder,updateReturnGoodsOrder,
  procureMaterialLotAssess, findUsedProcureMaterials, findUsedProcureMaterialLots
} from '../services/procure';
import {findDicMany} from "../services/personnel";

export default {
  namespace: 'procure',
  state: {},

  effects: {
    * create({payload}, {call, put}) {
      const response = yield call(create, payload);
      if (response) {
        return response;
      }
      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * findMany({payload}, {call, put}) {
      console.log('model findMany procure payload', payload);
      const response = yield call(findMany, payload);
      console.log('model findMany procure', response);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {orders: result}
        });
        return;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * findReturnOrderMany({payload}, {call, put}) {
      const response = yield call(findReturnOrderMany, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {returnOrders: result}});
      return response;
    },


    * findStatistic({payload}, {call, put}) {
      const response = yield call(findStatistic, payload);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            statistic: result
          }
        });

        return;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * findOne({payload}, {call, put}) {
      const response = yield call(findOne, payload);
      if (response) {
        const {result, status, message} = response;
        console.log('find on resule', result);
        if (status !== "ok" || !result) {
          yield put({type: 'onNotify', payload: {message, type: 'error'}});
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {procureOrder: result}
        });
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * clearOrder({payload}, {call, put}) {
      yield put({
        type: 'onDataLoad',
        payload: {
          procureOrder: undefined
        }
      });
    },
    * updateOne({payload}, {call, put}) {
      const response = yield call(updateOne, payload);
      if (response) {
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * deleteOne({payload}, {call, put}) {
      const response = yield call(deleteOne, payload);

      if (response) {
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * review({payload}, {call, put}) {
      const response = yield call(review, payload);
      if (response) {
        return response;
      }
      return {message: "操作失败，请检查网络！", type: 'error'};
    },
    * exportExcel({payload}, {call, put}) {
      const response = yield call(exportExcel, payload);
      if (response) {
        const {status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: message
          });
          return;
        }
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    /**
     * 查找可用的采购物料
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, void, *>}
     */
    * findUsedProcureMaterials({payload}, {call, put}) {
      const response = yield call(findUsedProcureMaterials, payload);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: message
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {useMaterials: result}
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
      }
    },

    /**
     * 查找可用的采购物料批次，根据物料查
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, void, *>}
     */
    * findUsedProcureMaterialLots({payload}, {call, put}) {
      const response = yield call(findUsedProcureMaterialLots, payload);
      console.log('rrrr', response);
      if (response) {
        const {result, status} = response;
        const usedProcureMaterialLots = status === 'ok' ? result : [];
        yield put({
          type: 'onDataLoad',
          payload: {usedProcureMaterialLots}
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
      }
    },
    * useMaterial({payload}, {call, put}) {
      const response = yield call(useMaterial, payload);
      if (response) {
        return response;
      }
      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    /**
     * 退货
     */
    * createReturnGoodsOrder({payload}, {call, put}) {
      const response = yield call(createReturnGoodsOrder, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      if (response.status !== "ok") {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result} = response;
      // yield put({type: 'onDataLoad', payload: {materialRecords: result}});
      return response;
    },

    * deleteReturnGoodsOrder({payload}, {call, put}) {
      const response = yield call(deleteReturnGoodsOrder, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      if (response.status !== "ok") {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result} = response;
      // yield put({type: 'onDataLoad', payload: {materialRecords: result}});
      return response;
    },

    * updateReturnGoodsOrder({payload}, {call, put}) {
      const response = yield call(updateReturnGoodsOrder, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      if (response.status !== "ok") {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result} = response;
      // yield put({type: 'onDataLoad', payload: {materialRecords: result}});
      return response;
    },
    * findMaterialRecordMany({payload}, {call, put}) {
      const response = yield call(findMaterialRecordMany, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      if (response.status !== "ok") {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result} = response;
      yield put({type: 'onDataLoad', payload: {materialRecords: result}});
    },


    * findProcureMaterialLot({payload}, {call, put}) {
      const response = yield call(findProcureMaterialLot, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      if (response.status !== "ok") {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result} = response;
      yield put({type: 'onDataLoad', payload: {materialLot: result}});
    },
    * procureMaterialLotAssess({payload}, {call, put}) {
      const response = yield call(procureMaterialLotAssess, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      if (response.status !== "ok") {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result} = response;
      yield put({type: 'onDataLoad', payload: {materialLotAssess: result}});
    },
    * findMaterialRecordByOrderCodeMany({payload}, {call, put}) {
      const {procureOrderCode} = payload;
      if (!procureOrderCode) {
        yield put({type: 'onDataLoad', payload: {materialRecords: []}});
        return;
      }
      const response = yield call(findMaterialRecordByOrderCodeMany, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      if (response.status !== "ok") {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result} = response;
      yield put({type: 'onDataLoad', payload: {materialRecords: result}});
    },
    * updateMaterial({payload}, {call, put}) {
      const response = yield call(updateMaterial, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      if (response.status !== "ok") {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result} = response;
      yield put({type: 'onDataLoad', payload: {materialRecords: result}});
    },
    * deleteMaterial({payload}, {call, put}) {
      const response = yield call(deleteMaterial, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      if (response.status !== "ok") {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result} = response;
      yield put({type: 'onDataLoad', payload: {materialRecords: result}});
    }
  },

  reducers: {
    onDataLoad(state, {payload}) {
      return {...state, ...payload};
    },
    onNotify(state, {payload}) {
      state._notify = payload;
      return {...state, ...payload};
    }
  }
};
